package  com.ruoyi.web.controller.product;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MedicinePlace;
import com.ruoyi.system.service.IMedicinePlaceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 中药登记场所详请Controller
 * 
 * @author yanhe
 * @date 2025-04-16
 */
@RestController
@RequestMapping("/system/place")
public class MedicinePlaceController extends BaseController
{
    @Autowired
    private IMedicinePlaceService medicinePlaceService;

    /**
     * 查询中药登记场所详请列表
     */
    @PreAuthorize("@ss.hasPermi('system:place:list')")
    @GetMapping("/list")
    public TableDataInfo list(MedicinePlace medicinePlace)
    {
        startPage();
        List<MedicinePlace> list = medicinePlaceService.selectMedicinePlaceList(medicinePlace);
        return getDataTable(list);
    }

    /**
     * 导出中药登记场所详请列表
     */
    @PreAuthorize("@ss.hasPermi('system:place:export')")
    @Log(title = "中药登记场所详请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MedicinePlace medicinePlace)
    {
        List<MedicinePlace> list = medicinePlaceService.selectMedicinePlaceList(medicinePlace);
        ExcelUtil<MedicinePlace> util = new ExcelUtil<MedicinePlace>(MedicinePlace.class);
        util.exportExcel(response, list, "中药登记场所详请数据");
    }

    /**
     * 获取中药登记场所详请详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:place:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(medicinePlaceService.selectMedicinePlaceById(id));
    }

    /**
     * 新增中药登记场所详请
     */
    @PreAuthorize("@ss.hasPermi('system:place:add')")
    @Log(title = "中药登记场所详请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MedicinePlace medicinePlace)
    {
        return toAjax(medicinePlaceService.insertMedicinePlace(medicinePlace));
    }

    /**
     * 修改中药登记场所详请
     */
    @PreAuthorize("@ss.hasPermi('system:place:edit')")
    @Log(title = "中药登记场所详请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MedicinePlace medicinePlace)
    {
        return toAjax(medicinePlaceService.updateMedicinePlace(medicinePlace));
    }

    /**
     * 删除中药登记场所详请
     */
    @PreAuthorize("@ss.hasPermi('system:place:remove')")
    @Log(title = "中药登记场所详请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(medicinePlaceService.deleteMedicinePlaceByIds(ids));
    }

    @PostMapping("/importData")
    public AjaxResult importData(@RequestPart("file") MultipartFile file) throws Exception {
            // 1. 校验文件基本格式
            if (file.isEmpty()) {
                throw new ServiceException("导入文件不能为空");
            }
            String filename = file.getOriginalFilename();
            if (!StringUtils.hasText(filename) ||
                    (!filename.toLowerCase().endsWith(".xls") && !filename.toLowerCase().endsWith(".xlsx"))) {
                throw new ServiceException("文件格式错误，请上传Excel文件");
            }

            // 2. 解析Excel数据
            ExcelUtil<MedicinePlace> util = new ExcelUtil<>(MedicinePlace.class);
            List<MedicinePlace> list = util.importExcel(file.getInputStream(), 0); // 0表示标题行索引

            // 3. 业务数据校验（示例）
            if (CollectionUtils.isEmpty(list)) {
                throw new ServiceException("导入数据为空");
            }
            for (MedicinePlace item : list) {
                // 示例校验逻辑，根据业务需求调整
                if (!StringUtils.hasText(item.getRegisterCode())) {
                    throw new ServiceException("场所编码不能为空");
                }
            }

            // 4. 写入数据库
            String result = medicinePlaceService.batchInsertMedicinePlace(list);

            return AjaxResult.success(result);
    }

}
